//
//  SDThreeAdvertisingView.m
//  Sydeny
//
//  Created by Rainbow on 7/24/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SDThreeAdvertisingView.h"
#import "UtilHelper.h"
#import "Advertising.h"
#import "ITSGetImageForButtonOperation.h"
#import "Advertising.h"

@implementation SDThreeAdvertisingView

-(id)initWithAds:(NSDictionary *)ads
{
    self = [super initWithAds:ads];
    if (self) {
        self.frame = CGRectMake(0, 0, 300, 210);
    }
    return self;
}

-(void)updateAds:(NSDictionary *)ads
{
    [super updateAds:ads];
    Advertising *advertising = [self.advertisings objectForKey:@"2"];
    if ([UtilHelper getImageFromDocument:advertising.image]) {
        UIImage *image = [UtilHelper getImageFromDocument:advertising.image];
        [firstAdButton setBackgroundImage:image forState:UIControlStateNormal];
    }
    else
    {
        ITSGetImageForButtonOperation *operation = [[ITSGetImageForButtonOperation alloc] initWithButton:firstAdButton imageUrl:[UtilHelper getFullImageUrl:advertising.image] nameToSave:advertising.image];
        [operationQueue addOperation:operation];
    }
}

-(void)loadComponents
{
    threeAd = YES;
    [super loadComponents];
    
    thirdAdButton = [[ImageButton alloc] initWithFrame:CGRectMake(0, 80, 300, 120)];
    [thirdAdButton setBackgroundImage:[UIImage imageNamed:@"600_240.png"] forState:UIControlStateNormal];
    [thirdAdButton addTarget:self action:@selector(tapOnAdvertising:) forControlEvents:UIControlEventTouchUpInside];
    thirdAdButton.layer.cornerRadius = 6;
    thirdAdButton.layer.masksToBounds = YES;
    thirdAdButton.tag = 3;
    [self addSubview:thirdAdButton];
    Advertising *advertising = [self.advertisings objectForKey:@"2"];
    if ([UtilHelper getImageFromDocument:advertising.image]) {
        UIImage *image = [UtilHelper getImageFromDocument:advertising.image];
        [thirdAdButton setBackgroundImage:image forState:UIControlStateNormal];
    }
    else
    {
        ITSGetImageForButtonOperation *operation = [[ITSGetImageForButtonOperation alloc] initWithButton:thirdAdButton imageUrl:[UtilHelper getFullImageUrl:advertising.image] nameToSave:advertising.image];
        [operationQueue addOperation:operation];
    }
}

- (void)dealloc
{
    [thirdAdButton release];
    thirdAdButton = nil;
    [super dealloc];
}

@end
